﻿// GoogleCode - http://code.google.com/p/outlook-4-you/
// GNU GPL v3 Licencing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using System.Diagnostics;
using System.IO;

namespace AutobuildAddIn
{
    public class BuildAddIn
    {
        private Office.CommandBar cb;
        private Office.CommandBarButton cbbRequest;
        private Office.CommandBarButton cbbLeave;
        private Office.CommandBarButton cbbBuild;
        private const string cb_name = "Autobuild";
        private Office.CommandBar cbt;
        private Office.CommandBarButton tbbCheck;
        private Outlook.AppointmentItem objApt;
        private Outlook.MailItem objMail;
        private Outlook.Inspectors objInspector;
        private Outlook.Items BuildItems;
        private Outlook.Folder olTasks;
        private double fLeave;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            olCal = this.Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
            olMail = this.Application.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            olBuilds = olMail.Parent.folders("Builds");
            olTasks = this.Application.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderTasks);
            objInspector = Application.Inspectors;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        public static bool Send_email(string too, string subj, string body)
        {
            Outlook.MailItem request = Globals.BuildAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem);
            try
            {
                // With...
                body.To = OlImportance.olImportanceHigh.Send();
                subj.Body = OlImportance.olImportanceHigh.Send();
                request.Subject = OlImportance.olImportanceHigh.Send();
                return true;
            }
            catch (SystemException ex)
            {
                MsgBox("Could not send email, Sorry");
                return false;
            }
        }

        public static bool Create_build(string refe, string stream, string sender, ref string GUID)
        {
            Outlook.TaskItem build = Globals.BuildAddIn.Application.CreateItem(Outlook.OlItemType.olTaskItem);
            try
            {
                string[] paras = stream.Split(",");
                string srcs = paras[3].Replace("$$", ("\r\n" + '\t'));
                string src = srcs.Replace("##", '\t');
                string comps = paras[4].Replace("$$", ("\r" + '\t'));
                string comp = comps.Replace("##", '\t');
                string sql = paras[5].Replace("$$", "\r\n");
                string imp = paras[6].Replace("$$", "\r\n");
                string info = paras[7].Replace("$$", "\r\n");
                int i = (sender.LastIndexOf("CN=") + 3);
                string sentname = sender.Substring(i, (sender.Length - i));
                string body = ("Sender: "
                            + (sender + ("\r\n" + ("ID: "
                            + (refe + ("\r\n" + ("Product: "
                            + (paras[0].ToString + ("\r\n" + ("Release: "
                            + (paras[1].ToString + ("\r\n" + ("Description: "
                            + (paras[2].ToString + ("\r\n" + ("Sourcefiles:" + ("\r\n" + ('\t'
                            + (src + ("\r\n" + ("Components: " + ("\r" + ('\t'
                            + (comp + ("\r" + ("SQLs: " + ("\r\n" + ('\t'
                            + (sql + ("\r\n" + ("Impact Analysis: " + ("\r"
                            + (imp + ("\r" + ("Additional Information: " + ("\r" + info))))))))))))))))))))))))))))))))))));
                // With...
                Now.AddDays(7).Status = OlImportance.olImportanceHigh.Save();
                Now.AddDays(7).ReminderTime = OlImportance.olImportanceHigh.Save();
                Now.DueDate = OlImportance.olImportanceHigh.Save();
                body.StartDate = OlImportance.olImportanceHigh.Save();
                ("Build Request - " + sentname.Body) = OlImportance.olImportanceHigh.Save();
                build.Subject = OlImportance.olImportanceHigh.Save();
                GUID = build.EntryID;
                return true;
            }
            catch (SystemException ex)
            {
                MsgBox(("Could not create build "
                                + (refe.Substring((refe.Length - 10), 10) + (", Sorry" + ("\r\n" + ex.Message)))));
                return false;
            }
        }

        private void Build_Check() {
        BuildItems = olTasks.Items;
        foreach (outlook.item item in BuildItems) {
            if ((item.GetType() == Outlook.TaskItem)) {
                object objTask = TryCast(item, Outlook.TaskItem);
                if (objTask!= null){
                    if (objTask.Subject.Contains("Build Request Accepted - ")) {
                        if ((objTask.Status == OlTaskStatus.olTaskInProgress)) {
                            // ' THIS IS WHERE THE BUILDING BEGINS
                            string[] paras = objTask.Body.Split("\r\n");
                            string ID = "";
                            for (i = 0; (i 
                                        <= (paras.Length - 1)); i++) {
                                if (paras[i].Contains("ID:")) {
                                    ID = paras[i].ToString;
                                }
                            }
                            MsgBox("Check Build environments");
                            MsgBox(("Building " + ID));
                        }
                    }
                }
            }
        }
    }

        //  Is a build ready to be started?
        private void Local_Compile_C()
        {
            Functional func = new Functional();
            func.Commandline("nmake /f Makefile > output.txt");
            string app_path = "C:\\output.txt";
            if ((func.ErrorCheck(app_path, "error") == true))
            {
                MsgBox("Compilation error");
            }
        }

        //  Standard local compile
        private void Putty_Compile_C() {
        Functional func = new Functional();
        string app_path = "C:\\";
        string bat_name = "Plink.bat";
        string cmd_name = "command.txt";
        string user = "bigdave@192.168.1.1";
        string pwd = "davebig";
        
        if ((func.CreateFile(bat_name, app_path) == true)) {
            using (StreamWriter sw1 = new StreamWriter(app_path + bat_name)
            {
                sw1.WriteLine("plink -ssh -u" + user + " -p" + pwd + " -m " + app_path + cmd_name);
            }
        }
        if ((func.CreateFile(cmd_name, app_path) == true)) {
            using (StreamWriter sw2 = new StreamWriter(app_path + cmd_name))
            {
                sw2.WriteLine("gmake -f Makefile 1>output.txt 2>error.txt");
            }
        }
        func.Commandline("Plink.bat");
    }

        private void WeekStarts(ref DateTime Start, ref DateTime dEnd)
        {
            switch (DateAndTime.Now.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    Start = new DateTime(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day, 0, 0, 0);
                    dEnd = DateAndTime.Now.AddDays(6);
                    break;
                case DayOfWeek.Tuesday:
                    Start = new DateTime(DateAndTime.Now.Year, DateAndTime.Now.Month, (DateAndTime.Now.Day - 1), 0, 0, 0);
                    dEnd = DateAndTime.Now.AddDays(5);
                    break;
                case DayOfWeek.Wednesday:
                    Start = new DateTime(DateAndTime.Now.Year, DateAndTime.Now.Month, (DateAndTime.Now.Day - 2), 0, 0, 0);
                    dEnd = DateAndTime.Now.AddDays(4);
                    break;
                case DayOfWeek.Thursday:
                    Start = new DateTime(DateAndTime.Now.Year, DateAndTime.Now.Month, (DateAndTime.Now.Day - 3), 0, 0, 0);
                    dEnd = DateAndTime.Now.AddDays(3);
                    break;
                case DayOfWeek.Friday:
                    Start = new DateTime(DateAndTime.Now.Year, DateAndTime.Now.Month, (DateAndTime.Now.Day - 4), 0, 0, 0);
                    dEnd = DateAndTime.Now.AddDays(2);
                    break;
                case DayOfWeek.Saturday:
                    Start = new DateTime(DateAndTime.Now.Year, DateAndTime.Now.Month, (DateAndTime.Now.Day - 5), 0, 0, 0);
                    dEnd = DateAndTime.Now.AddDays(1);
                    break;
                case DayOfWeek.Sunday:
                    Start = new DateTime(DateAndTime.Now.Year, DateAndTime.Now.Month, (DateAndTime.Now.Day - 6), 0, 0, 0);
                    dEnd = DateAndTime.Now;
                    break;
            }
        }
    }
}